package com.song.controller.admin;
import com.song.constant.JwtClaimsConstant;
import com.song.dto.EmployeeLoginDTO;
import com.song.dto.EmployeePageQueryDTO;
import com.song.dto.EmployeeRegisterDTO;
import com.song.entity.Employee;
import com.song.properties.JwtProperties;
import com.song.result.PageResult;
import com.song.result.Result;
import com.song.service.EmployeeService;
import com.song.utils.JwtUtil;
import com.song.vo.EmployeeLoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/admin/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;
    //员工登录接口
    @PostMapping("/login")
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO)
    {
        log.info("员工登录信息：用户名{},密码{}",employeeLoginDTO.getUsername(),employeeLoginDTO.getPassword());
        Employee employee = employeeService.login(employeeLoginDTO);
        //登录成功后生成jwt令牌
        Map<String,Object> claims = new HashMap();
        claims.put(JwtClaimsConstant.EMP_ID,employee.getId());
        String token = JwtUtil.generateJwt(jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims);

       return Result.success(EmployeeLoginVO.builder()
               .id(1L)
               .userName(employee.getUsername())
               .name(employee.getName())
               .token(token)
               .build(),"登录成功");
    }
    //添加员工接口
    @PostMapping
    public Result addEmployee(@RequestBody EmployeeRegisterDTO employeeRegisterDTO){
        log.info("员工添加接口，员工信息{}",employeeRegisterDTO);
        employeeService.addEmployee(employeeRegisterDTO);
        return Result.success();
    }

    //员工信息分页查询
    @GetMapping("/page")
    public Result<PageResult<Employee>> getEmployeePage(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("员工信息分页查询接口，查询参数,name:{},页码:{},每页大小:{}",
                employeePageQueryDTO.getName(),employeePageQueryDTO.getPage(),employeePageQueryDTO.getPageSize());
       return Result.success(employeeService.getEmployeePage(employeePageQueryDTO),"查询成功");
    }

    //根据id查询员工信息
    @GetMapping("/{id}")
    public Result<Employee> getEmployeeById(@PathVariable Long id){
        log.info("根据id查询员工信息接口,员工id:{}",id);
        return Result.success(employeeService.getEmployeeById(id),null);
    }

    //修改员工账号信息
    @PutMapping
    public Result updateEmployee(@RequestBody Employee employee){
        log.info("修改员工账号信息接口,员工信息{}",employee);
        Integer result = employeeService.updateEmployee(employee);
        return Result.success() ;
    }

    //修改员工账号状态
    @PostMapping("/status/{status}")
    public Result updateStatus(@PathVariable Integer status,@RequestParam Long id){
        log.info("员工账号状态修改接口,员工状态:{}",status);
        Integer result = employeeService.updateEmployeeStatus(status,id);
        if(result==1) return Result.success(null,"员工状态修改成功");
        return Result.error("员工状态修改失败");
    }
}
